/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:43
 */

package com.lenovo.mso2o.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.lenovo.framework.base.ActionBarFragmentActivity;
import com.lenovo.framework.base.ActionBarInterface;
import com.lenovo.framework.base.InjectableFragment;
import com.lenovo.framework.entity.Res;
import com.lenovo.framework.rest.NetworkObserver;
import com.lenovo.framework.util.AppHelper;
import com.lenovo.framework.util.TextHelper;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.conf.MSConstants;
import com.lenovo.mso2o.entity.PasswordApplyingRequest;
import com.lenovo.mso2o.rest.ServiceGenerator;
import com.lenovo.mso2o.rest.UserAPI;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by lit on 2015/11/16.
 */
public class ChangepassFragment extends InjectableFragment implements ActionBarInterface {
    @Bind(R.id.newpass)
    EditText newPassEdi;
    @Bind(R.id.newpassagin)
    EditText newPassAginEdi;
    private Subscription subscription;
    private String aesPassword;

    @OnClick(R.id.next)
    void Next() {
        if (!TextUtils.isEmpty(newPassEdi.getText().toString())
                && !TextUtils.isEmpty(newPassAginEdi.getText().toString())
                && TextHelper.isPWD(newPassEdi.getText().toString())) {
            if (newPassEdi.getText().toString().equals(newPassAginEdi.getText().toString())) {

                String newPass = newPassEdi.getText().toString();
                try {
                    aesPassword = AppHelper.encrypt("`h:LKoj$#Hi&T@h$8i%n^k-c#l~o^u*n+d", newPass);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                PasswordApplyingRequest req = new PasswordApplyingRequest();
                req.setLoginname(null);
                req.setSecuritycode(null);
                subscription = userAPI.confirmRequest(req).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .unsubscribeOn(Schedulers.io())
                        .subscribe(new NetworkObserver<Res>() {
                            @Override
                            public void onSuccess(Res res) {
                                if (res.getEcode() == 0) {
                                    Bundle bundle = new Bundle();
                                    bundle.putString(ActionBarFragmentActivity.FRAGMENT_CLASS_NAME, ChangePassSucessFragment.class.getName());
                                    bundle.putInt(ActionBarFragmentActivity.FRAGMENT_TITLE, R.string.changepass_success);
                                    Intent intent = new Intent(getActivity(), ActionBarFragmentActivity.class);
                                    intent.putExtras(bundle);
                                    startActivity(intent);
                                } else {
                                    Toast.makeText(getActivity(), res.getReason(), Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onFailure(String tip) {
                                Toast.makeText(getActivity(), getString(R.string.change_pass_faild), Toast.LENGTH_SHORT).show();
                            }
                        });
            } else {
                Toast.makeText(getActivity(), getString(R.string.Two_input_password), Toast.LENGTH_SHORT).show();
//                showLongToast(getString(R.string.Two_input_password));
            }
        } else {
            Toast.makeText(getActivity(), getString(R.string.pass_Format_error), Toast.LENGTH_SHORT).show();
//            showLongToast(getString(R.string.pass_Format_error));
        }
    }

    private UserAPI userAPI;

    public ChangepassFragment() {
        // Required empty public constructor
    }

    //    String  AuthCode;
    String PhoneNumber;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        Bundle data = getArguments();
//        AuthCode=data.getString("Code");
        PhoneNumber = data.getString("Phone");
        userAPI = ServiceGenerator.createService(UserAPI.class, MSConstants.APIConstants.APISERVER_ADDRESS);
        return view;
    }

    @Override
    public int provideContentRes() {
        return R.layout.fragment_chagepass;
    }

    @Override
    protected void onFragmentInVisible(Bundle savedInstanceState) {

    }

    @Override
    protected void onFragmentVisible(Bundle savedInstanceState) {

    }

    @Override
    public int getActionBarRightResourceId() {
        return 0;
    }

    @Override
    public int getActionBarRight2ResourceId() {
        return 0;
    }

    @Override
    public void onActionBarRight2Click() {

    }

    @Override
    public void onActionBarRightClick() {

    }

    @Override
    public boolean onBackPressed() {
        return false;
    }

}
